#!/usr/bin/env bash
DIR="${BASH_SOURCE%/*}"; source $DIR/servers.sh

clean=0
pubapp=0
selserver=

#------------------------------------------
#read the options
opts=$(getopt -o kshcp:: --long server:: -- "$@")
eval set -- "$opts"
while true; do
    case "$1" in
        -h|-\?)
            echo "-c make clean before publish"
            echo "-p really publish to server"
            exit 0
            ;;

        -k) 
            sshkey=1; shift ;;

        -s) 
            sshlogin=1; shift ;;

        -c) 
            clean=1; shift ;;

        -p|--server)
            pubapp=1
             case "$2" in 
                 * ) selserver=$2 ;;
             esac
             shift 2;;

        --) shift; break;;
        *) echo "error"; exit ;;
    esac
done
#echo $pubapp,$selserver,$clean; exit
#------------------------------------------

function read_server {
    if [ -z $selserver ]; then
        echo "已配置的服务器:"
        for i in "${!serverConfigs[@]}"
        do
            v=${serverConfigs[$i]}
            IFS='/' read -r -a cfg <<< "$v"
            echo $i: ${cfg[1]}@${cfg[0]}
        done
        echo "----------"
        echo "请选择服务器:"
        read selserver
    fi

    serverCfg=${serverConfigs[$selserver]}
    IFS='/' read -r -a cfg <<< "$serverCfg"
    if [ -z $serverCfg ]; then
        echo "不存在的服务器:",$selserver
        exit 1
    fi
    echo "select server: $selserver, $serverCfg"

    server=${cfg[0]}
    user=${cfg[1]}
}

function make_pub {
    dcfile=docker-compose.deploy.yml
    scp $dcfile $user@$server:$dcfile 
    ssh $user@$server "docker-compose -f $dcfile pull"
    ssh $user@$server "docker-compose -f $dcfile restart"
}

if [ x$sshlogin == x1 ]; then
    read_server
    ssh $user@$server $@

elif [ x$sshkey == x1 ]; then
    for i in "${!serverConfigs[@]}"
    do
        v=${serverConfigs[$i]}
        IFS='/' read -r -a cfg <<< "$v"
        server=${cfg[0]}
        user=${cfg[1]}
        echo "addkey to: "$i: ${user}@${server}
        ssh-copy-id $user@$server
    done

elif [ x$pubapp == x1 ]; then

    echo "ci:"$ci
    if [ -n "$ci"  ]; then
        echo "public to: ${ciPubServers[@]}"
        for i in "${ciPubServers[@]}"
        do
            v=${serverConfigs[$i]}
            IFS='/' read -r -a cfg <<< "$v"
            server=${cfg[0]}
            user=${cfg[1]}
            echo "do: "$i: ${user}@${server}
            make_pub
        done

    elif [ "$selserver" != @ ]; then
        read_server
        echo "3秒后发布到:$selserver, $serverCfg"
        sleep 1; echo 2; sleep 1; echo 1; sleep 1
        make_pub

    else
        logfile=data/pub.log
        echo "3秒后发布到所有服务器"
        sleep 1; echo 2; sleep 1; echo 1; sleep 1
        echo "begin pub">$logfile
        for i in "${!serverConfigs[@]}"
        do
            v=${serverConfigs[$i]}
            IFS='/' read -r -a cfg <<< "$v"
            server=${cfg[0]}
            user=${cfg[1]}
            echo "do: "$i: ${user}@${server}
            echo "do: "$i: ${user}@${server} >>$logfile
            make_pub&
        done
    fi

fi
